using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColorPiece : MonoBehaviour
{
    public enum ColorType{
        YELLOW,
        PURELE,
        RED,
        GREEN,
        ANY,
        COUNT
    }
    [System.Serializable]
    public struct ColorSprite
    {
        public ColorType color;
        public Sprite sprite;
    }
    public ColorSprite[] colorSprites;
    private ColorType color;
    public int NumColors
    {
        get { return colorSprites.Length-2 ; }
    }
    private Dictionary<ColorType, Sprite> colorSpriteDict;

    public ColorType Color { get => color; set => SetColor( value); }
    private SpriteRenderer sprite;


    private void Awake()
    {
        sprite = transform.Find("FruitImg").GetComponent<SpriteRenderer>();
        colorSpriteDict = new Dictionary<ColorType, Sprite>();
        for(int i = 0; i < colorSprites.Length; i++)
        {
            if (!colorSpriteDict.ContainsKey(colorSprites[i].color))
            {
                colorSpriteDict.Add(colorSprites[i].color, colorSprites[i].sprite);
            }
        }
    }
    public void SetColor(ColorType newColor)
    {
        color = newColor;
        if (colorSpriteDict.ContainsKey(newColor))
        {
            sprite.sprite = colorSpriteDict[newColor];
        }
    }
}
